package cn.utils;

import java.util.ArrayList;
import java.util.List;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Stage;

public class InjectorCreator {
	
	private static Injector injector=null;

	private InjectorCreator(){
		
	}
	
	
	/**
	 * 返回Injector  默认方法,只添加了事务TransactionModule
	 * @return
	 */
	public static Injector getInjector(){
		return getInjector(false);
	}
	
	/**
	 * 返回Injector  默认方法,只添加了事务TransactionModule
	 * @param fresh 强迫重新创建injector
	 * @return
	 */
	public static Injector getInjector(Boolean fresh){
		
		return getInjector(fresh, null);
	}
	
	
	/**
	 * 返回Injector  默认方法,只添加了事务TransactionModule
	 * @param fresh 强迫重新创建injector
	 * @return
	 */
	public static Injector getInjector(Boolean fresh,List<Module> modules){
		return getInjector(fresh, modules, null);
	}
	
	/**
	 * 返回Injector  默认方法,只添加了事务TransactionModule
	 * @param fresh 强迫重新创建injector
	 * @return
	 */
	public static Injector getInjector(Boolean fresh,List<Module> modules,Stage stage){
		if(injector == null || fresh){
			if(stage==null){
				injector = createInjector(modules);
			}else{
				injector = createInjector(stage,modules);
			}
		}
		return injector;
	}
	
	
	
	/**
	 * 默认方法,只添加了事务TransactionModule
	 * @return
	 */
	protected static Injector createInjector(){
		Injector injector = Guice.createInjector();
		return injector;
	}
	
	/**
	 * 添加指定modules
	 * @return
	 */
	protected static Injector createInjector(List<Module> modules){
		if(modules==null){
			modules=new ArrayList<Module>();
		}
		//modules.add(new TransactionModule());
		
		Injector injector = Guice.createInjector(modules);
		return injector;
	}
	
	
	/**
	 * 指定stage,添加指定modules
	 * @return
	 */
	protected static Injector createInjector(Stage stage,List<Module> modules){
		if(modules==null){
			modules=new ArrayList<Module>();
		}
		//modules.add(new TransactionModule());
		
		Injector injector = Guice.createInjector(stage,modules);
		return injector;
	}
}
